﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;

namespace PWDirCreat
{
    public class CreatHander
    {
        private List<string> FileList;//文本文件读到的文件列表
        private readonly string abdSeed = AppDomain.CurrentDomain.BaseDirectory + Path.DirectorySeparatorChar + "Seed" + Path.DirectorySeparatorChar + "DesignSeed.dgn";
        private readonly string opmSeed = AppDomain.CurrentDomain.BaseDirectory + Path.DirectorySeparatorChar + "Seed" + Path.DirectorySeparatorChar + "pmseed3d.dgn";
        private string RootPath;
        private List<string> pathList;//需要创建的目录，需要对这个排序
        private List<string> dgnInsFileList;//需要创建的仪表文件
        private List<string> dgnTubFileList;//需要创建的仪表管文件

        /// <summary>
        /// 检查种子文件是否存在
        /// </summary>
        /// <returns></returns>
        public bool CheckSeed()
        {

            if (File.Exists(abdSeed) && File.Exists(opmSeed))
            {
                return true;
            }

            return false;
        }
        /// <summary>
        /// 读取TXT文本
        /// </summary>
        /// <returns></returns>
        public void DesigneFileList()
        {
            FileList = new List<string>();
            string txtFile = AppDomain.CurrentDomain.BaseDirectory + Path.DirectorySeparatorChar + "list.txt";
            FileStream fs = new FileStream(txtFile, FileMode.Open);
            StreamReader reader = new StreamReader(fs, UnicodeEncoding.ASCII);
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                line = line.Trim().ToString();
                if (!FileList.Contains(line))
                {
                    Console.WriteLine(line);
                    FileList.Add(line);
                }

            }
            fs.Close();


        }
        public void DataHanld()
        {
            pathList = new List<string>();
            dgnInsFileList = new List<string>();
            dgnTubFileList = new List<string>();
            RootPath = AppDomain.CurrentDomain.BaseDirectory + Guid.NewGuid().ToString();
            Console.WriteLine("*********************************************************");
            Console.WriteLine("开始分析数据");
            for (int i = 0; i < FileList.Count; i++)
            {
                string filename = FileList[i].ToUpper();

                Console.WriteLine("处理文件名：" + filename);
                FileNameHanld(filename);

            }
            //添加总装文件夹 仪表管的总装文件

            dgnInsFileList.Add(RootPath + Path.DirectorySeparatorChar + "总装文件" + Path.DirectorySeparatorChar + "厂房分装.dgn");
            dgnInsFileList.Add(RootPath + Path.DirectorySeparatorChar + "总装文件" + Path.DirectorySeparatorChar + "设备分装.dgn");
            dgnTubFileList.Add(RootPath + Path.DirectorySeparatorChar + "总装文件" + Path.DirectorySeparatorChar + "仪表管分装.dgn");
        }
        /// <summary>
        /// 创建文件
        /// </summary>
        public void Creat()
        {
            Directory.CreateDirectory(RootPath);//创建根文件夹
            Directory.CreateDirectory(RootPath + Path.DirectorySeparatorChar + "总装文件");//创建根文件夹
            Directory.CreateDirectory(RootPath + Path.DirectorySeparatorChar + "仪表管");//创建根文件夹
            //创建文件夹
            foreach (string item in pathList)
            {
                Directory.CreateDirectory(item);
            }
            //创建ABD用的文件

            foreach (string item in dgnInsFileList)
            {
                File.Copy(abdSeed, item);
            }
            foreach (string item in dgnTubFileList)
            {
                File.Copy(opmSeed, item);
            }

        }
        private void FileNameHanld(string fileName)
        {
            if (fileName.Contains("TM") || fileName.Contains("FM") || fileName.Contains("SM"))
            {
                return;
            }
            if (fileName.Contains("YKS06") || fileName.Contains("YKS07"))
            {
                //仪表管，只需要复制文件
                string tempFileName = RootPath + Path.DirectorySeparatorChar + "仪表管" + Path.DirectorySeparatorChar + fileName.Substring(0, 13) + ".dgn";
                if (!dgnTubFileList.Contains(tempFileName))
                {
                    dgnTubFileList.Add(tempFileName);

                }

            }
            else if (fileName.Contains("YKS03") || fileName.Contains("YKS05"))
            {
                // 其他类型，需要创建目录,总装文件
                string tempPath = RootPath + Path.DirectorySeparatorChar + fileName.Substring(0, 13);//创建的目录
                string tempFileName = tempPath + Path.DirectorySeparatorChar + fileName + ".dgn";
                string tempZZFileName = RootPath + Path.DirectorySeparatorChar + "总装文件" + Path.DirectorySeparatorChar + fileName.Substring(0, 13) + ".dgn";
                if (!pathList.Contains(tempPath))
                {
                    pathList.Add(tempPath);
                }
                if (!dgnInsFileList.Contains(tempPath))
                {
                    dgnInsFileList.Add(tempFileName);
                }
                if (!dgnInsFileList.Contains(tempZZFileName))
                {
                    dgnInsFileList.Add(tempZZFileName);
                }

            }
            else
            {
                //只创建总装文件
                string tempFileName = RootPath + Path.DirectorySeparatorChar + "总装文件" + Path.DirectorySeparatorChar + fileName.Substring(0, 13) + ".dgn";
                if (!dgnInsFileList.Contains(tempFileName))
                {
                    dgnInsFileList.Add(tempFileName);

                }


            }
        }
        //给文件夹添加序号
        public void GetpathIndex()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(string));
            dt.Columns.Add("NEWID", typeof(string));

            foreach (string item in pathList)
            {
                dt.Rows.Add(Path.GetFileName(item), item);
            }
            dt.DefaultView.Sort = "ID";
            dt = dt.DefaultView.ToTable();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                CopyDir(dt.Rows[i][1].ToString(), Path.GetDirectoryName(dt.Rows[i][1].ToString()) + Path.DirectorySeparatorChar + (i + 1).ToString() + "-" + dt.Rows[i][0].ToString());
                DeleteFolder(dt.Rows[i][1].ToString());
            }

        }

        private void CopyDir(string srcPath, string aimPath)
        {
            try
            {
                // 检查目标目录是否以目录分割字符结束如果不是则添加之
                if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
                    aimPath += Path.DirectorySeparatorChar;
                // 判断目标目录是否存在如果不存在则新建之
                if (!Directory.Exists(aimPath))
                    Directory.CreateDirectory(aimPath);
                // 得到源目录的文件列表，该里面是包含文件以及目录路径的一个数组
                // 如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
                string[] fileList = Directory.GetFileSystemEntries(srcPath);
                // 遍历所有的文件和目录
                foreach (string file in fileList)
                {
                    // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                    if (Directory.Exists(file))
                    {
                        CopyDir(file, aimPath + Path.GetFileName(file));
                    }
                    // 否则直接Copy文件
                    else
                    {
                        File.Copy(file, aimPath + Path.GetFileName(file), true);
                    }
                }

            }
            catch
            {

            }
        }
        private void DeleteFolder(string deleteDirectory)
        {
            if (Directory.Exists(deleteDirectory))
            {
                foreach (string deleteFile in Directory.GetFileSystemEntries(deleteDirectory))
                {
                    if (File.Exists(deleteFile))
                        File.Delete(deleteFile);
                    else
                        DeleteFolder(deleteFile);
                }
                Directory.Delete(deleteDirectory);
            }
        }
    }

}
